package com.dataStructure.树;

import com.dataStructure.model.Person;
import com.dataStructure.树.AVL树.AVLTree;
import com.dataStructure.树.AVL树.BinarySearchTree;
import com.dataStructure.树.printer.BinaryTrees;
import com.dataStructure.树.printerFile.Files;
import com.dataStructure.树.二叉搜索树.二叉搜索树_BinarySearchTree;
import org.junit.jupiter.api.Test;

public class TreeTest {
    private static class PersonComparator implements java.util.Comparator<Person> {
        @Override
        public int compare(Person e1, Person e2) {
            return e1.getAge() - e2.getAge();
        }
    }

    private static class PersonComparator2 implements java.util.Comparator<Person> {
        @Override
        public int compare(Person e1, Person e2) {
            return e2.getAge() - e1.getAge();
        }
    }

    @Test
    public void test1() {
        Integer[] data = new Integer[]{7, 4, 9, 2, 5, 8, 11, 3};

        二叉搜索树_BinarySearchTree<Integer> bst = new 二叉搜索树_BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }

        BinaryTrees.println(bst);

        System.out.println();

    }

    @Test
    public void test2() {
        //传入不同的比较器，可以实现不同的比较逻辑
        二叉搜索树_BinarySearchTree<Person> bst2 = new 二叉搜索树_BinarySearchTree<>(/*new PersonComparator()*/);
        bst2.add(new Person(12));
        bst2.add(new Person(54));
        bst2.add(new Person(85));
        bst2.add(new Person(4));
        bst2.add(new Person(74));
        bst2.add(new Person(124));
        bst2.add(new Person(45));
        bst2.add(new Person(2));
        BinaryTrees.println(bst2);
    }

    @Test
    public void test3() {
        二叉搜索树_BinarySearchTree<Person> bst2 = new 二叉搜索树_BinarySearchTree<>(new PersonComparator2());
        bst2.add(new Person(54));
        bst2.add(new Person(27));
        bst2.add(new Person(12));
        bst2.add(new Person(4));
        bst2.add(new Person(57));
        bst2.add(new Person(123));
        bst2.add(new Person(7));
        bst2.add(new Person(45));
        BinaryTrees.println(bst2);
    }

    @Test
    public void test4() {
        二叉搜索树_BinarySearchTree<Integer> binarySearchTree = new 二叉搜索树_BinarySearchTree<>();
        for (int i = 0; i < 50; i++) {
            binarySearchTree.add((int) (Math.random() * 100));
        }
        BinaryTrees.println(binarySearchTree);
        String s = BinaryTrees.printString(binarySearchTree);
        Files.writeToFile("F:\\1.txt",s+"\n",true);
    }
    @Test
    public void test5(){
        Integer[] data = new Integer[]{7, 4, 9, 2, 5, 8, 11, 3};

        二叉搜索树_BinarySearchTree<Integer> bst = new 二叉搜索树_BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);
        System.out.println(bst.height1());
    }
    @Test
    public void test6(){
        Integer[] data = new Integer[]{7, 4, 9, 2, 5, 8, 11, 3};

        二叉搜索树_BinarySearchTree<Integer> bst = new 二叉搜索树_BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);
        System.out.println(bst);
        System.out.println(bst.height());
        System.out.println(bst.height1());
    }
    @Test
    public void test7(){
        Integer[] data = new Integer[]{7, 4, 9, 2, 5};

        二叉搜索树_BinarySearchTree<Integer> bst = new 二叉搜索树_BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);
        System.out.println(bst.isComplete());
    }
    @Test
    public void test8(){
        Integer[] data = new Integer[]{7, 4, 9, 2, 5,8,1,6,12,85,4,1,63};

        二叉搜索树_BinarySearchTree<Integer> bst = new 二叉搜索树_BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);
        bst.remove(7);
        BinaryTrees.println(bst);
    }

    @Test
    public void test9(){
        BinarySearchTree<Integer>  bst = new BinarySearchTree();

        Integer[] data = new Integer[]{1,2,3,4,5,6,7,8,9,10};
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);

        AVLTree<Integer> avlTree = new AVLTree<>();
        for (int i = 0; i < data.length; i++) {
            avlTree.add(data[i]);
        }
        BinaryTrees.println(avlTree);
    }

    @Test
    public void test10(){

    }
}
